import request from '@/utils/request.js'

/**
 * 订单池
 */
export function getOrderList (longitude, latitude) {
	return request.get({
		url: `/driver/getOrderPool`,
		params: {
			longitude,
			latitude
		}
	})
}

/**
 * 抢单
 */
export function grabOrder (orderId) {
	return request.post({
		url: '/driver/grabOrder',
		params: {
			orderId
		}
	})
}

/**
 * 查询司机全部订单
 */
export function getAllMyOrderList ({ pageNum, pageSize, status }) {
	const params = {
		pageNum,
		pageSize
	}
	if (status) {
		params['status'] = status
	}
	// console.log('🔍 查询订单请求参数:', params)
	return request.get({
		url: `/driver/getAllOrder`,
		params
	}).then(res => {
        // console.log('📦 查询订单响应:', res)
        return res
    })
}

/**
 * 更新订单状态
 */
export function updateStatus ({ id, status }) {
	return request.post({
		url: '/order/updateSts',
		data: {
			id,
			status
		}
	})
}

/**
 * 司机取消订单
 */
export function cancelOrder (id) {
	return request.post({
		url: '/driver/cancel',
		data: { id }
	})
}

/**
 * 根据订单ID查询订单
 */
export function getOrderById (id) {
	return request.get({
		url: `/order/getById?orderId=${id}`
	})
}

/**
 * 司机完成订单
 */
export function finishOrder ({ orderId, payType }) {
	return request.post({
		url: '/order/driver_finish_order',
		data: {
			orderId,
			payType
		}
	})
}

/**
 * 查询价格
 */
export function calcPriceByDistance (km) {
	return request.get({
		url: `/order/getPrice?km=${km}`
	})
}

/**
 * 查询司机是否有订单正在进行
 */
export function checkDriverOrder (driverId) {
	return request.get({
		url: `/order/checkDriverOrder?driverId=${driverId}`
	})
}
/**
 * 司机创建订单
 */
export function driverCreate (orderData) {
	return request.post({
		url: '/order/driverCreate',
		data: orderData
	})
}